package com.android.jianboshi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.util.DateUtil;
import com.android.jianboshi.R;
import com.android.jianboshi.bean.LogInfo;
import com.blankj.utilcode.util.ConvertUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */

public class LogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    List<LogInfo> list;
    CommCallBack callBack;

    public LogAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<LogInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_loglist, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final LogInfo info = list.get(position);
        //数据绑定逻辑
        viewHolder.tv_text1.setText(DateUtil.getDateFromTimeLine(info.time,"HH:mm:ss.SSS"));

        if(position!=0)
        {
            long timeTemp = info.time - list.get(position-1).time;
            viewHolder.tv_text2.setText(timeTemp+" ms");
        }
        viewHolder.tv_text3.setText(info.airPressure+"");
        viewHolder.tv_text4.setText(info.samplingTimes+"");
//        SpannableStringBuilder spannable = new SpannableStringBuilder(info.hex);
//        spannable.setSpan(new ForegroundColorSpan(Color.RED),46,50, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        viewHolder.tv_text5.setText(ConvertUtils.bytes2HexString(new byte[]{info.num}));


        viewHolder.tv_hex.setText(info.hex);
        viewHolder.tv_hex.setVisibility(View.GONE);
        if(info.airPressure>0)
        {
            viewHolder.tv_text3.setTextColor(Color.RED);
        }
        else
        {
            viewHolder.tv_text3.setTextColor(Color.WHITE);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onResult(info);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_text1,tv_text2,tv_text3,tv_hex,tv_text4,tv_text5;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_text1 = itemView.findViewById(R.id.tv_text1);
            tv_text2 = itemView.findViewById(R.id.tv_text2);
            tv_text3 = itemView.findViewById(R.id.tv_text3);
            tv_hex = itemView.findViewById(R.id.tv_hex);
            tv_text4 = itemView.findViewById(R.id.tv_text4);
            tv_text5= itemView.findViewById(R.id.tv_text5);
        }
    }
}
